<?php

namespace Home\Controller;

class CsvController extends CommonController {

    public function index() {

        $this->display();
    }

    public function import() {
        $filename = $_FILES['file']['tmp_name'];
//        print_r($_FILES);
        if (empty($filename)) {
            echo '请选择要导入的CSV文件！';
            exit;
        }
        $handle = fopen($filename, 'r');
        $result = input_csv($handle); //解析csv 
        $len_result = count($result);
        if ($len_result == 0) {
            echo '没有任何数据！';
            exit;
        }
        for ($i = 1; $i < $len_result; $i++) { //循环获取各字段值 
            $name = iconv('gb2312', 'utf-8', $result[$i][0]); //中文转码 
            $sex = iconv('gb2312', 'utf-8', $result[$i][1]);
//            $age = $result[$i][2];
            $data_values .= "('$name','$sex'),";
        }
        echo $data_values;exit;
        $data_values = substr($data_values, 0, -1); //去掉最后一个逗号 
        fclose($handle); //关闭指针 
        M("csv")->query("insert into csv (num,email) values $data_values");
        echo M("csv")->getlastsql();
//        $query = mysql_query("insert into student (name,sex,age) values $data_values"); //批量插入数据表中 
//        if ($query) {
//            echo '导入成功！';
//        } else {
//            echo '导入失败！';
//        }
    }

}

?>
